package com.controller;

import com.google.gson.Gson;
import pojo.Amount;
import pojo.TickTable;
import pojo.TransferTable;
import service.BillService;
import service.TickService;
import service.impl.BillServiceImpl;
import service.impl.TickServiceImpl;
import util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/bill/*")
public class BillServlet extends HttpServlet {
    private BillService billService=new BillServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String url = request.getRequestURI();
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        Class cla = BillServlet.class;
        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(), request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        boolean boo=billService.login(account,password);
        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(boo));
    }
    public void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        ArrayList<TransferTable> show = billService.show();
        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(show));
    }
    public void showQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Amount amount=new Amount();
        amount.setMinAcount(0.0);
        amount.setMaxAcount(Double.MAX_VALUE);
        TransferTable transferTable=new TransferTable();
        String no = request.getParameter("no");
        String time = request.getParameter("opentime");
        String recive = request.getParameter("recive");
        String open = request.getParameter("open");
        String min = request.getParameter("min");
        String max = request.getParameter("max");
        //String successful = request.getParameter("successful");
        //String transfering = request.getParameter("transfering");
        //String lose = request.getParameter("lose");
        //String str=request.getParameter("str");

        /*if(str.equals(successful)){
            transferTable.setStatus("转让中");
        }else if(str.equals(transfering)){
            transferTable.setStatus("转让中");
        }else if(str.equals(lose)){
            transferTable.setStatus("失败");
        }*/


        if("0".equals(recive)){
            transferTable.setCompanyname("请选择收单企业");
        }else if("a".equals(recive)){
            transferTable.setCompanyname("百度科技有限公司");
        }else if("b".equals(recive)){
            transferTable.setCompanyname("京东集团");
        }else if("c".equals(recive)){
            transferTable.setCompanyname("小米");
        }
        if("0".equals(open)){
            transferTable.setEname("请选择开单企业");
        }else if("d".equals(open)){
            transferTable.setEname("腾讯科技有限公司");
        }else if("e".equals(open)){
            transferTable.setEname("隆基股份有限公司");
        }
        transferTable.setNo(no);
        transferTable.setOpentime(time);
        if(min.length()>0&&max.length()>0){
            amount.setMinAcount(Double.parseDouble(min));
            amount.setMaxAcount(Double.parseDouble(max));
        }
        PageHelper pageHelper=new PageHelper();
        ArrayList<TransferTable> list=billService.selectQuery(transferTable,amount,pageHelper);
        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(list));
    }
    //全部展示我的凭证
    private TickService tickService= new TickServiceImpl();
    public void showTicket(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ArrayList<TickTable> show= tickService.show();
        System.out.println(show);
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(show));
    }
}
